package com.br4mmie.asciistory.ui;

import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;
import com.br4mmie.asciistory.Keybindings;
import com.br4mmie.asciistory.mission.Inventory;
import com.br4mmie.asciistory.mission.InventoryItem;
import com.br4mmie.asciistory.mission.Item;


/**
 * The InventoryWindow shows your inventory
 * @author Bram Buurlage
 */
public class InventoryWindow extends Window {
	
	private Inventory inventory;
	private int selection;
	
	/**
	 * constructor
	 * @param inventory the underlying inventory to show
	 */
	public InventoryWindow(Inventory inventory)
	{
		// Inventory scherm coordinaten
		super(2, 2, 52, 14);
		this.inventory = inventory;
		selection = -1;
	   
	}
	
	/**
	 * draws info about an item
	 * @param item the item to draw info about
	 * @param x the x location to draw the info at
	 * @param y the y location to draw the info at
	 * @param w the maximum width of the text (for now, use 19)
	 */
	private void drawItemInfo(Item item, int x, int y, int w)
	{
		image.drawText(x, y++, w, "-----Item info-----");
		y+=image.drawText(x, y++, w, item.getName())+1;
		image.drawText(x, y++, w, "-------------------");
		y+=image.drawText(x, y++, w, item.getDescription())+1;
		image.drawText(x, y++, w, "-------------------");
		y+=image.drawText(x, y++, w, "Price: "+item.getGoldValue())+1;
		image.drawText(x, y++, w, "-------------------");
	}
	
	/**
	 * draws the inventorywindow
	 * @param target the target image to draw to
	 */
	public void draw(CharImage target)
	{	
		image.fill(' ');
		
		image.drawText(26, 2, 32, "Gold:");
		image.drawText(26, 3, 32, ""+inventory.getGold());
		image.drawText( 1, image.getHeight()-4, 32, "movement keys:  select item");
		image.drawText( 1, image.getHeight()-3, 32, Keybindings.INV_USE+":");
		image.drawText(17, image.getHeight()-3, 32, "use item");
		image.drawText( 1, image.getHeight()-2, 32, Keybindings.INV_REM+":");
		image.drawText(17, image.getHeight()-2, 32, "drop item");
		image.drawText( 1, image.getHeight()-1, 32, Keybindings.INV_EQP+":");
		image.drawText(17, image.getHeight()-1, 32, "equip item");
		
		int n = inventory.size();
		for(int y = 0; y<2; y++)
			for(int x = 0; x<5; x++)
			{
				int i = x+y*5;
				if(i < n)
				{
					Item item = inventory.get(i).item;
					item.getImage().draw(image, 2 + x*5, 1+y*2, 0, 0, 1, 1);
					
					String amountStr = ""+inventory.get(i).amount;
					image.putChar(amountStr.charAt(0), 3+x*5, 1+y*2);          /* 2+x*3, 1+y*2 */
				}
				
				//selectie haakjes
				if (i == selection)
				{
					image.putChar('(', 1+x*5, 1+y*2);
					image.putChar(')', 4+x*5, 1+y*2);
					
					if (i < n) drawItemInfo(inventory.get(i).item, image.getWidth()-20, 1, 19);
				}
				
				//Inventory mooi maken met strepen ertussen
				image.putChar('|', 5+x*5, 1+y*2);
				
				image.putChar('-', 1+x*5, 2+y*2);
				image.putChar('-', 2+x*5, 2+y*2);
				image.putChar('-', 3+x*5, 2+y*2);
				image.putChar('-', 4+x*5, 2+y*2);
				image.putChar('+', 5+x*5, 2+y*2);
			}
		
		super.draw(target);
	}
	
	/**
	 * a LOT of stuff is handled here.
	 * let's list it.
	 * - using the 0-9 keys you can select an item
	 * - using the U key you can call the use method of an item
	 * - using WASD you can select items also.
	 */
	public void handleEvents()
	{		
		//item gebruiken
		if (Keybindings.INV_USE.pressed())
		{
			InventoryItem sel = getSelection();
			if (sel !=  null) 
			{
				if(sel.item.use())
				{
					inventory.removeItem(sel);
				}
			}
		}
		if (Keybindings.INV_EQP.pressed())
		{
			InventoryItem sel = getSelection();
			if ( sel != null) inventory.equipItem(sel);
		}
		if (Keybindings.INV_REM.pressed())
		{
			InventoryItem sel = getSelection();
			if (sel != null) {inventory.dropItem(sel);try{Thread.sleep(500);}catch (Exception ex){}}
		}
		
		try 
		{
			if (Keybindings.RIGHT.pressed() && selection < 9)
			{
				selection++;
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
			if (Keybindings.LEFT.pressed() && selection > 0)
			{
				selection--;
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
			if (Keybindings.UP.pressed() && selection >= 5)
			{
				selection -= 5;
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
			if (Keybindings.DOWN.pressed() && selection <= 4)
			{
				selection += 5;
				Thread.sleep(ASCIIStoryActivity.keyDelay);
			}
		} catch (Exception ex) {}
		
		getInput();
	}
	
	/**
	 * returns the current selection
	 */
	public InventoryItem getSelection()
	{
		if (selection < 0) return null;
		else if (inventory.size() > selection) return inventory.get(selection);
		return null;
	}

}
